<?php
/*
 * ebiz front
 *
 * applygiftService Service
 *
 * @author allen
 *
 */

require_once '../app/core/services/BaseService.php';

require_once '../app/constants/ConfigConstants.php';

require_once '../app/models/ProductDao.php';
require_once '../app/models/ProductItemDao.php';
require_once '../app/models/GiftDao.php';
require_once '../app/models/ApplyGiftLogDao.php';
require_once '../app/models/PromotionDao.php';
require_once '../app/models/PromotionConditionDao.php';

class ApplyGiftService extends BaseService
{

	private $productDao;
	private $productItemDao;
	private $giftDao;
	private $applyGiftLogDao;
	private $promotionDao;
	private $promotionConditionDao;

	public function __construct(){
		parent::__construct();
		$this->productDao = new ProductDao($this->dbUtil);
		$this->productItemDao = new ProductItemDao($this->dbUtil);
		$this->giftDao = new GiftDao($this->dbUtil);
		$this->applyGiftLogDao = new ApplyGiftLogDao($this->dbUtil);
		$this->promotionDao = new PromotionDao($this->dbUtil);
		$this->promotionConditionDao = new PromotionConditionDao($this->dbUtil);
	}

	/**
	 * 求出礼品记录数分页用
	 */
	public function getAllNum(){
		$giftsInfos = $this->giftDao->getAllNum();
        $queryAllNum = $giftsInfos[0]["allNum"];
        return $queryAllNum;
	}
	
    /**
     * 兑换礼品，如果礼品是网站中的商品，根据pid显示出产品的信息
     * $int_page设置默认为第一页
     */
    public function getGiftProductInfo($int_page = 1){   
        $result = array();
        $itemsPerPage = $GLOBALS["config"]['items_per_page'];//akimimi set is 20
    	$giftsInfos = $this->giftDao->getAllGiftsByAvailabilityTime( ( ($int_page - 1) * $itemsPerPage ), $itemsPerPage );
    	foreach ($giftsInfos as $giftsInfo) {
    		$itemId = $giftsInfo["product_item_id"];
    		if(!empty($itemId)){//礼品是网店中的产品
    			$itemInfos = $this->productItemDao->getById($itemId);
    			$productId = $itemInfos[0]["product_id"];
    			$productInfos = $this->productDao->getGiftProductByPid($productId);
    			$productInfo = $productInfos[0];
    			//生成图片全路径
    			if( !empty($productInfo["folder"]) && !empty($productInfo["name"]) ){
    				$imagePath = ConfigConstants::getProductImagePath($productInfo["folder"], $productInfo["name"], 60);
    			}else{
    				$imagePath = ConfigConstants::getDefaultProductImagePath(60);
    			}
    			
    			$result[] = array(
    			                 "id" => $giftsInfo["id"],
                                 "exhaust_points" => $giftsInfo["exhaust_points"],
                                 "min_ac_points" => $giftsInfo["min_ac_points"],
//                                 "product_item_id" => $giftsInfo["product_item_id"],
                                 "title" => $productInfo["title"],
                                 "description" => "",
                                 "url_title" => $productInfo["url_title"],
                                 "image_path" => $imagePath,
                                 "start_data" => $giftsInfo["start_date"],
                                 "end_data" => $giftsInfo["end_date"]
    			             );
    			
    		}else{//礼品是网店中没有的产品信息都从gifts表中取
    			$result[] = array(
                                 "id" => $giftsInfo["id"],
                                 "exhaust_points" => $giftsInfo["exhaust_points"],
                                 "min_ac_points" => $giftsInfo["min_ac_points"],
//                                 "product_item_id" => $giftsInfo["product_item_id"],
                                 "title" => $giftsInfo["title"],
                                 "description" => $giftsInfo["description"],
                                 "url_title" => "",
                                 "image_path" => $giftsInfo["image_path"],
                                 "start_data" => $giftsInfo["start_date"],
                                 "end_data" => $giftsInfo["end_date"]
                             );
    		}
    	}
        return $result;
    }
    
    /**
     * 插入日志信息
     * $type=1兑换抵用卷
     * $type=2兑换礼品
     * $exhaustPoints==points礼品的兑换积分
     * $points_buyer==current_exhaust_points当前用户兑换积分总额
     * $points_buyer_total==current_ac_points当前用户晋级积分
     */
    public function saveApplyGiftLog($type, $giftId, $exhaustPoints, $points_buyer, $points_buyer_total){
    	$apply_type = $type;
    	$user_id = AAService::getCustomer();
    	$points = $exhaustPoints;
    	$current_exhaust_points = $points_buyer;
    	$current_ac_points = $points_buyer_total;
    	//$giftId
    	$status = 1;
    	$delivery_name = HttpUtil::getPostString("addUserName");
    	$delivery_phone1 = HttpUtil::getPostString("addPhone");
    	$delivery_phone2 = HttpUtil::getPostString("addMobileTelephone");
    	$delivery_address1 = HttpUtil::getPostString("province");
    	$delivery_address2 = HttpUtil::getPostString("city");
    	$delivery_address3 = HttpUtil::getPostString("county");
    	$delivery_detail_address = HttpUtil::getPostString("addOtherAddress");
    	$delivery_email = HttpUtil::getPostString("addEmail");
    	$param = array($apply_type, $user_id, $points, $current_exhaust_points, $current_ac_points, $giftId, $status, $delivery_name, 
    	$delivery_phone1, $delivery_phone2, $delivery_address1, $delivery_address2, $delivery_address3,$delivery_detail_address, $delivery_email);
    	
    	return $this->applyGiftLogDao->saveLogs($param);
    }
    
    public function saveApplyCouponLog($type, $exhaustPoints, $points_buyer, $points_buyer_total, $promotionId, $code_value){
    	$apply_type = $type;
    	$user_id = AAService::getCustomer();
    	$points = $exhaustPoints;
    	$current_exhaust_points = $points_buyer;
    	$current_ac_points = $points_buyer_total;
        $status = 5;
    	$param = array($apply_type, $user_id, $points, $current_exhaust_points, $current_ac_points, $status, $promotionId, $code_value);
    	
    	return $this->applyGiftLogDao->saveCouponLogs($param);
    }
    
    /**
     * 显示兑换抵用券的promotion信息
     */
    public function getExchangePromotion(){
    	$result = array();
    	$exchangePromotions = $this->promotionDao->getExchangePromotion();
    	foreach ($exchangePromotions as $exchangePromotion) {
    		$id = $exchangePromotion["id"];
    		$short_description = $exchangePromotion["short_description"];
            $min_ac_points = $exchangePromotion["min_ac_points"];
    		$exhaust_points = $exchangePromotion["exhaust_points"];
	        $result[] = array(
	                       "id" => $id,
	                       "short_description" => $short_description,
	                       "min_ac_points" => $min_ac_points,
	                       "exhaust_points" => $exhaust_points
	        );
    	}
    	return $result;
    }
    
    /**
     * 保存兑换抵用券信息
     * promotion_condition表
     */
    public function savePromotionConditionInfo($param){
    	$this->promotionConditionDao->saveExchangePromotionConditionInfo($param);
    } 
    
    /**
     * 显示兑换信息
     */
    public function showExchangeInfo($int_page = 1){
    	$logList = array();
    	$userId = AAService::getCustomer();
    	$itemsPerPage = $GLOBALS["config"]['items_per_page'];//akimimi set is 20
    	$result = $this->applyGiftLogDao->getApplyGiftInfo( ( ($int_page - 1) * $itemsPerPage ), $itemsPerPage, $userId );
    	foreach ($result as $item) {
    		if($item["apply_type"] == 1){//兑换抵用券
    			$promotionId = $item["promotion_id"];
    			$promotionInfo = $this->promotionDao->getPromotionById($promotionId);
    			$logList[] = array(
    			                     "id" => $item["id"],
    			                     "apply_type" => $item["apply_type"],
    			                     "user_id" => $item["user_id"],
    			                     "points" => $item["points"],
    			                     "current_exhaust_points" => $item["current_exhaust_points"],
    			                     "current_ac_points" => $item["current_ac_points"],
    			                     "promotion_id" => $item["promotion_id"],
    			                     "coupon_code" => $item["coupon_code"],
    			                     "status" => $item["status"],
    			                     "promotion_id" => $item["promotion_id"],
                                     "created" => $item["created"],
    			                     "title" => $promotionInfo[0]["short_description"]
//    			                     "min_ac_points" => $promotionInfo[0]["min_ac_points"],
//    			                     "exhaust_points" => $promotionInfo[0]["exhaust_points"]
    			);
    		}else{//兑换礼品
    			$giftInfo = $this->giftDao->getGiftsById($item["gift_id"]);
    			$productInfo = $this->productDao->getGiftProductByPid($giftInfo[0]["product_item_id"]);
    			$title = empty($giftInfo[0]["title"]) ? $productInfo[0]["title"] : $giftInfo[0]["title"];
    			$logList[] = array(
                                     "id" => $item["id"],
                                     "apply_type" => $item["apply_type"],
                                     "user_id" => $item["user_id"],
                                     "points" => $item["points"],
                                     "current_exhaust_points" => $item["current_exhaust_points"],
                                     "current_ac_points" => $item["current_ac_points"],
                                     "status" => $item["status"],
                                     "created" => $item["created"],
                                     "gift_id" => $item["gift_id"],
                                     "start_date" => $giftInfo[0]["start_date"],
                                     "end_date" => $giftInfo[0]["end_date"],
                                     "title" => $title
                );
    		}
    	}
    	return $logList;
    	
    }
    
    public function getAllNumber(){
    	$userId = AAService::getCustomer();
    	$result = $this->applyGiftLogDao->getAllNumber($userId);
    	return $result[0]["allNum"];
    }
    
    
     /**
      * Steve
     * 通过ID获取申请礼品日志信息
     */
    public function getApplyLogById($id){
    	return $this->applyGiftLogDao->getApplyLogById($id);
    }
    
    public function getPromotionInfoById($promotionId){
    	return $this->promotionDao->getPromotionInfoById($promotionId);
    }
    
    public function getGiftById($giftId){
    	return $this->giftDao->getGiftById($giftId);
    }

}







